package com.cluster.platform.service.impl;

import com.cluster.platform.common.api.CommonResult;
import com.cluster.platform.dao.UmsAdminDao;
import com.cluster.platform.dao.UmsUserDetailDao;
import com.cluster.platform.dto.UmsUserDetailDto;
import com.cluster.platform.mapper.UmsAdminMapper;
import com.cluster.platform.mapper.UmsUserDetailMapper;
import com.cluster.platform.model.UmsAdmin;
import com.cluster.platform.model.UmsUserDetail;
import com.cluster.platform.service.UmsUserDetailService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;

/**
 * @ClassName: UmsUserDetailServiceImpl
 */
@Service
public class UmsUserDetailServiceImpl implements UmsUserDetailService {

    @Autowired
    private UmsUserDetailDao umsUserDetailDao;

    @Autowired
    private UmsUserDetailMapper umsUserDetailMapper;

    @Autowired
    private UmsAdminDao umsAdminDao;

    /**
     * 查询用户信息
     *
     * @param userId
     * @return
     */
    @Override
    public CommonResult<UmsUserDetailDto> searchByUserId(String userId) {
        if (StringUtils.isBlank(userId)) {
            return CommonResult.failed("未获取查询参数");
        }
        UmsUserDetailDto umsUserDetailDto = umsUserDetailDao.searchByUserId(userId);
        if (null == umsUserDetailDto) {
            return CommonResult.failed("未找到该用户详细信息");
        }
        return CommonResult.success(umsUserDetailDto);
    }

    /**
     * 根据用户名查询用户信息
     * @param username
     * @return
     */
    @Override
    public CommonResult<UmsUserDetailDto> searchByUsername(String username) {
        if (StringUtils.isBlank(username)) {
            return CommonResult.failed("未获取参数");
        }
        UmsUserDetailDto umsUserDetailDto = umsUserDetailDao.searchByUsername(username);
        if ( null == umsUserDetailDto) {
            return CommonResult.failed("未查询到该用户信息");
        }
        return CommonResult.success(umsUserDetailDto);
    }

    /**
     * 更新用户表单信息
     *
     * @param umsUserDetailDto
     * @return
     */
    @Override
    @Transactional
    public CommonResult<UmsUserDetailDto> updateUserDetail(UmsUserDetailDto umsUserDetailDto) {
        if (null == umsUserDetailDto) {
            return CommonResult.failed("未获取客户信息");
        }
        UmsUserDetail umsUserDetail = new UmsUserDetail();
        umsUserDetail.setUserId(umsUserDetailDto.getUserId());
        umsUserDetail.setSex(umsUserDetailDto.getSex());
        umsUserDetail.setPhone(umsUserDetailDto.getPhone());
        umsUserDetail.setQq(umsUserDetailDto.getQq());
        umsUserDetail.setWechat(umsUserDetailDto.getWechat());
        umsUserDetail.setAddress(umsUserDetailDto.getAddress());
        umsUserDetail.setDepartments(umsUserDetailDto.getDepartments());
        umsUserDetail.setGrade(umsUserDetailDto.getGrade());
        umsUserDetail.setStudentId(umsUserDetailDto.getStudentId());
        int updateUmsUserDetail = umsUserDetailDao.updateByUserId(umsUserDetail);
        if (updateUmsUserDetail < 1) {
            return CommonResult.failed("更新用户详情信息失败");
        }
        UmsAdmin umsAdmin = new UmsAdmin();
        if (!StringUtils.isBlank(umsUserDetailDto.getUsername())) {
            umsAdmin.setUsername(umsUserDetailDto.getUsername());
        }
        if (!StringUtils.isBlank(umsUserDetailDto.getIcon())) {
            umsAdmin.setIcon(umsUserDetailDto.getIcon());
        }
        if (!StringUtils.isBlank(umsUserDetailDto.getEmail())) {
            umsAdmin.setEmail(umsUserDetailDto.getEmail());
        }
        if (!StringUtils.isBlank(umsUserDetailDto.getNickName())) {
            umsAdmin.setNickName(umsUserDetailDto.getNickName());
        }
        if (StringUtils.isBlank(umsAdmin.getUsername()) &&
                StringUtils.isBlank(umsAdmin.getEmail()) &&
                StringUtils.isBlank(umsAdmin.getNickName())) {
            return CommonResult.success(umsUserDetailDto);
        }
        int updateUmsAdmin = umsAdminDao.updateByUserName(umsAdmin);
        if (updateUmsAdmin < 0) {
            return CommonResult.failed("更新用户检验信息失败");
        }
        return CommonResult.success(umsUserDetailDto);
    }

    /**
     * 删除用户详细信息
     *
     * @param userId
     * @return
     */
    @Override
    @Transactional
    public CommonResult<Object> deleteUserDetail(String userId) {
        if (StringUtils.isBlank(userId)) {
            return CommonResult.failed("未获取到用户数据");
        }
        int logic = umsUserDetailDao.logicToDelete(userId);
        if (logic < 0) {
            return CommonResult.failed("删除失败");
        }
        return CommonResult.success("用户详细信息已删除");
    }

    /**
     * 批量删除
     *
     * @param umsAdmins
     * @return
     */
    @Override
    @Transactional
    public CommonResult<Object> batchDeleteUserDetail(List<UmsAdmin> umsAdmins) {
        int update = umsAdminDao.batchDeleteByUserId(umsAdmins);
        if (update < 0) {
            return CommonResult.failed("批量删除失败");
        }
        return CommonResult.success(update);
    }

    /**
     * 新增用户详细信息
     */
    @Override
    @Transactional
    public CommonResult<UmsUserDetailDto> insertUmsUserDetail(UmsUserDetailDto umsUserDetailDto) {
        if (null == umsUserDetailDto) {
            return CommonResult.failed("未获取用户信息");
        }
        if (StringUtils.isBlank(umsUserDetailDto.getUsername())) {
            return CommonResult.failed("用户名为空");
        }
        UmsAdmin umsAdmin = umsAdminDao.searchByUsername(umsUserDetailDto.getUsername());
        if (null == umsAdmin) {
            return CommonResult.failed("为查询到可用账户信息");
        }
        UmsUserDetail umsUserDetail = new UmsUserDetail();
        umsUserDetail.setUserId(String.valueOf(umsAdmin.getId()));
        umsUserDetail.setId(String.valueOf(System.currentTimeMillis()));
        umsUserDetail.setSex(umsUserDetailDto.getSex());
        umsUserDetail.setPhone(umsUserDetailDto.getPhone());
        umsUserDetail.setQq(umsUserDetailDto.getQq());
        umsUserDetail.setWechat(umsUserDetailDto.getWechat());
        umsUserDetail.setAddress(umsUserDetailDto.getAddress());
        umsUserDetail.setDepartments(umsUserDetailDto.getDepartments());
        umsUserDetail.setGrade(umsUserDetailDto.getGrade());
        umsUserDetail.setStudentId(umsUserDetailDto.getStudentId());
        umsUserDetail.setIntroduce(umsUserDetailDto.getIntroduce());
        umsUserDetail.setInsertDate(new Date());
        umsUserDetail.setStatus(1);
        int insert = umsUserDetailMapper.insertSelective(umsUserDetail);
        if (insert < 0) {
            return CommonResult.failed("插入失败");
        }
        return CommonResult.success(umsUserDetailDto);
    }

}
